import { createRouter, createWebHashHistory } from 'vue-router'
// import { umsThirdUserlogin } from '@/api/index'

const routes = [
  {
    path: '/login',
    component: () => import('@/views/loginView.vue')
  },
  {
    path: '/home',
    component: () => import('@/views/homeView.vue'),
    name: 'homeView',
    meta: {
      requiresAuth: true,
      requiresAdmin: true // 需要身份验证
    },
    children: [
      {
        path: 'everyView',
        component: () => import('@/views/everyView.vue'),
        name: 'everyView',
        meta: {
          requiresAuth: true,
          requiresAdmin: true // 需要身份验证
        },
        children: [
          {
            path: 'shopList',
            component: () => import('@/views/shop/shopList.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'checkSheet',
            component: () => import('@/views/orderManagement/checkSheet.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'shopType',
            component: () => import('@/views/shop/shopType.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'returnOrder',
            component: () => import('@/views/orderManagement/returnOrder.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },

          {
            path: 'afterSale',
            component: () => import('@/views/orderManagement/afterSale.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'preSales',
            component: () => import('@/views/orderManagement/preSales.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'awaitAudit',
            component: () => import('@/views/storeManagement/awaitAudit.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'awaitPurchase',
            component: () => import('@/views/storeManagement/awaitPurchase.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'purchasedButNoGo',
            component: () => import('@/views/storeManagement/purchasedButNoGo.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'awaitQualityTesting',
            component: () => import('@/views/storeManagement/awaitQualityTesting.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'inspectedAwaitDispose',
            component: () => import('@/views/storeManagement/inspectedAwaitDispose.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'awaiStock',
            component: () => import('@/views/storeManagement/awaiStock.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'ReturnToWarehouse',
            component: () => import('@/views/storeManagement/ReturnToWarehouse.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'WarehousingSuccess',
            component: () => import('@/views/storeManagement/WarehousingSuccess.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'goReturnToWarehouse',
            component: () => import('@/views/storeManagement/goReturnToWarehouse.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'marketOutOrder',
            component: () => import('@/views/OutboundManagement/marketOutOrder.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'destroyMessageList',
            component: () => import('@/views/OutboundManagement/destroyMessageList.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'retrunOrder',
            component: () => import('@/views/OutboundManagement/retrunOrder.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'SignFailure',
            component: () => import('@/views/OutboundManagement/SignFailure.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'allPage',
            component: () => import('@/views/inventoryVerification/allPage.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'awaiCtounted',
            component: () => import('@/views/inventoryVerification/awaiCtounted.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'acknowledgedCtounted',
            component: () => import('@/views/inventoryVerification/acknowledgedCtounted.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'shiftList',
            component: () => import('@/views/inventoryVerification/shiftList.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'awaitAuditInentory',
            component: () => import('@/views/inventoryVerification/awaitAuditInentory.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'warehouseList',
            component: () => import('@/views/warehouseManagement/warehouseList.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'warehouseAdd',
            component: () => import('@/views/warehouseManagement/warehouseAdd.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'ExportInformation',
            component: () => import('@/views/InformationTem/ExportInformation.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'templeManagement',
            component: () => import('@/views/InformationTem/templeManagement.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'warehouseType',
            component: () => import('@/views/warehouseManagement/warehouseType.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'warehouseGoodsArea',
            component: () => import('@/views/warehouseManagement/warehouseGoodsArea.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'warehouseSeat',
            component: () => import('@/views/warehouseManagement/warehouseSeat.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'rookieExpress',
            component: () => import('@/views/expressManagement/rookieExpress.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'basicSetting',
            component: () => import('@/views/systemSettings/basicSetting.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'MergeLog',
            component: () => import('@/views/logquery/MergeLog.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'printSetup',
            component: () => import('@/views/systemSettings/printSetup.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            },
            children: [
              {
                path: 'expressSheet',
                component: () => import('@/views/systemSettings/expressSheet.vue'),
                meta: {
                  requiresAuth: true,
                  requiresAdmin: true // 需要身份验证
                }
              },
              {
                path: 'expressSheetTable',
                component: () => import('@/views/systemSettings/expressSheetTable.vue'),
                meta: {
                  requiresAuth: true,
                  requiresAdmin: true // 需要身份验证
                }
              }
            ]
          },
          {
            path: 'GroupingList',
            component: () => import('@/views/systemSettings/GroupingList.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'userSetting',
            component: () => import('@/views/systemSettings/userSetting.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'PurchasingAccountList',
            component: () => import('@/views/purchaseSetup/PurchasingAccountList.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'PurchasingAccountPlatform',
            component: () => import('@/views/purchaseSetup/PurchasingAccountPlatform.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'PurchasingAccountType',
            component: () => import('@/views/purchaseSetup/PurchasingAccountType.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'stallStockAlready',
            component: () => import('@/views/stallStock/stallStockAlready.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'stallStockAwaitGet',
            component: () => import('@/views/stallStock/stallStockAwaitGet.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'stallStockAwaitSet',
            component: () => import('@/views/stallStock/stallStockAwaitSet.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'stallList',
            component: () => import('@/views/stallStock/stallList.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'afterSaleScan',
            component: () => import('@/views/WarehousePrinting/afterSaleScan.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'prePurchasePrint',
            component: () => import('@/views/WarehousePrinting/prePurchasePrint.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          },
          {
            path: 'paymentID',
            component: () => import('@/views/purchaseSetup/paymentID.vue'),
            meta: {
              requiresAuth: true,
              requiresAdmin: true // 需要身份验证
            }
          }

        ]
      },
      {
        path: 'shopDetails',
        component: () => import('@/views/shop/shopDetails.vue'),
        meta: {
          requiresAuth: true,
          requiresAdmin: true // 需要身份验证
        }
      },
      {
        path: 'a',
        component: () => import('@/views/NotView.vue'),
        meta: {
          requiresAuth: true,
          requiresAdmin: true // 需要身份验证
        }
      },
      {
        path: 'getPrePickOrderDetail',
        component: () => import('@/views/orderManagement/getPrePickOrderDetail.vue'),
        meta: {
          requiresAuth: true,
          requiresAdmin: true // 需要身份验证
        }
      },
      {
        path: 'indexConfiguration',
        component: () => import('@/views/orderConfiguration/indexConfiguration.vue'),
        meta: {
          requiresAuth: true,
          requiresAdmin: true // 需要身份验证
        }
      }
    ]
  },

  {
    path: '/',
    redirect: '/login'
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
const authMap = {
  '/home': 'Yt_Operate_gelidan',
  '/usercontrol': 'Yt_Operate_User'
}
// 全局前置守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = checkUserAuthentication()
  console.log(isAuthenticated)
  if (router.options.routes.some(to => to.path)) {
    if (to.matched.some(route => route.meta.requiresAuth)) {
      if (!isAuthenticated) {
        next('/login') // 登
      } else {
        // if (localStorage.getItem('system_token_cloudWare')) {
        const requiredPermission = authMap[to.path]
        if (isAuthenticated && checkUserAdminStatus(requiredPermission)) {
          next() // 权
        } else if (requiredPermission) {
          next(false) // 不足
        } else {
          next()
        }
        // } else {
        //   console.log('11111---noLogin')
        //   umsThirdUserlogin({
        //     consoleToken: isAuthenticated
        //     // consoleToken: 'b5066053c18f2c92c6507c6667906df11717750828355773e67c6ef1f7631eeac34daf75bc'
        //   }).then(res => {
        //     console.log('tokenres')
        //     console.log(res)
        //     localStorage.setItem('system_token_cloudWare', res.data.data.token)
        //     next()
        //   })
        // }
      }
    } else {
      next()
    }
  }
})
function checkUserAuthentication () {
  return localStorage.getItem('system_token')
}
function checkUserAdminStatus (val) {
  const arrays = JSON.parse(localStorage.getItem('system_data'))
  if (!arrays) {
    return false
  } else if (arrays.id === 1) {
    return true
  } else {
    return arrays.auth_key_arr.includes(val)
  }
}

export default router
